﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Surface.Presentation;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Common.FileSystem;
using ScrumTable.Common.Threading;
using ScrumTable.Common.Unity;
using ScrumTable.UI.View.Controls;
using ScrumTable.UI.View.Extension;
using ScrumTable.UI.View.Localization;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.View.WinTouch.UserControls.Tool;
using ScrumTable.UI.ViewModel;

#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls
{
    /// <summary>
    /// Interaction logic for SurfaceUserControl1.xaml
    /// </summary>
    public partial class AttachmentControl : IDropUserControl
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private readonly ClickUtil _click = new ClickUtil();
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
 


        #endregion

        #region Constructors / Destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="UserStoryControl"/> class.
        /// </summary>
        public AttachmentControl()
        {
            DataContextChanged += OnDataContextChanged;
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                InitializeComponent();
                FontSize = 10;
                NameLabel.FontWeight = FontWeights.Normal;
            }
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        private void ShowBackground(InputEventArgs e)
        {
            if (PresentationSource.FromVisual(this) == null)
            {
                return;
            }

            if (!_click.IsClickInTimeAndPlace(e))
                return;

            var window = (AppScreen)Application.Current.MainWindow;
            
            if (((AttachmentViewModel)DataContext).IsImage)
            {
                window.AddUriImageToScatterItem(((AttachmentViewModel)DataContext).Uri);
            }
            else
            {
                ShowAttachment(window);
            }
        }

        public void ShowAttachment(AppScreen window)
        {
            string filePathRel;
            string filePathAbs;
            var attachment = ((AttachmentViewModel)DataContext);
            if (attachment.Uri == null) { return; }

            FileProcessor.GetFilePathAbsAndRel(out filePathRel, out filePathAbs, attachment.Original.Extension);
            BackgroundRoot.Background = new VisualBrush(new WaitControl() { Width = ActualWidth, Height = ActualHeight });
            
            new FileProcessor(attachment.Original.Context.ConnectionInformation.NetworkCredentials,
                              window.Container.Resolve<IDataSyncDispatcher>(), attachment.Uri, filePathAbs,
                              () => { BackgroundRoot.Background = Brushes.Gray; },
                              (hasChanged) =>
                                  {
                                      if (hasChanged)
                                      {
                                          var name = attachment.Name;
                                          attachment.Uri = new Uri(filePathAbs);
                                          attachment.Name = name;
                                      }
                                  });
        }
        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------


        private void OnSurfaceButtonClick(object sender, RoutedEventArgs e)
        {
            if (((AttachmentViewModel)DataContext).IsImage)
            {
                ScrumInkCanvas.CreateAndShowInkCanvas(DataContext as AttachmentViewModel);
            }
            else
            {
                ShowAttachment(Application.Current.MainWindow as AppScreen);
            }
        }

        private void OnUserStoryPreviewTouchDown(object sender, TouchEventArgs e)
        {
            ShowBackground(e);
        }


        private void OnUserStoryPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            ShowBackground(e);
        }

        private void OnAttachementDrop(object sender, SurfaceDragDropEventArgs e)
        {
           
        }

        private void OnUserStoryPreviewQueryTarget(object sender, QueryTargetEventArgs e)
        {
            if (CanDrop(e.Cursor.Data) != CanDropResult.CanDrop)
            {
                e.UseDefault = false;
                e.ProposedTarget = null;
                e.Handled = true;
            }
        }


        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (DataContext is Attachment)
            {
                DataContext = new AttachmentViewModel((Attachment)DataContext);
            }
        }

        #endregion



        #region IDropUserControl Members

        public FrameworkElement GetRootControl
        {
            get { return DropRoot; }
        }

        public bool ShowIconWithbackground
        {
            get { return true; }
        }

        public CanDropResult CanDrop(object objectToDrop)
        {
            return this.ConvertBoolToEnum( objectToDrop is UserStoryViewModel || objectToDrop is BugViewModel || objectToDrop is TaskViewModel);
        }

        #endregion

       

        
    }
}
